<?php
/**
 * This file is part of the Agora distribution. 
 * Detailed copyright and licensing information can be found
 * in the gpl-3.0.txt file which should be included in the distribution.
 * 
 * @version		$Id: 3.0.14 Olympus 2010-09-28 rammstein4o $
 * @copyright  2007 - 2010 jVitals
 * @license   GPLv3 Open Source
 * @link       http://jvitals.com
 * @since      File available since initial release
 */
	defined ('IN_AGORA') or die;

	class TaskController extends AgoraPostController
	{
		function _default()
		{
			$post_model = & Model::getInstance('PostModel');
			$post_id = Agora::getRequestVar('post_id');
			$post_id = (int)$post_id ? $post_id : $this->id;
			$post = $post_model->load($post_id);
			
			$can_edit = $this->access_model->authenticatePost($this->agora_user['id'], $post['id'],'edit_posts');
			
			// member cannot edit other users posts
			if (!$this->agora_user['is_superadmin'] && !$can_edit && $post['poster_id'] != $this->agora_user['id']) {
				Agora::showMessage(Agora::lang('Access denied'));
				Agora::redirect(Agora::getRefferer());
			}

			$topic_model = & Model::getInstance('TopicModel');
			$forum_id = $topic_model->getForumId($post['topic_id']);
			
			$catnav_helper = & $this->helper('catnav');
			$catnav_helper->fromForum($forum_id);

			$this->view->assign('post_id',$post['id']);
			$this->view->assign('post_body', AString::escape($post['message']));
			$this->view->assign('topic_username', AString::escape((string) @$post['topic_username']));
			$this->view->assign('guest_email', AString::escape((string) @$post['guest_email']));
			$this->view->template = 'post/edit';
		}

		function unsaved()
		{
			$this->view->assign('topic_id',(int)Agora::getPostVar('id'));
			$this->view->assign('post_body',Agora::getPostVar('req_message','',true));
			$this->view->assign('topic_username', Agora::getPostVar('topic_username',''));
			$this->view->assign('guest_email', Agora::getPostVar('guest_email',''));
			$this->view->assign('edit_unsaved',1);
			$this->view->template = 'post/edit';
		}

		function preview()
		{
			$raw_message = Agora::getPostVar('req_message','',true);
			$message = $raw_message;
			$hide_smilies = Agora::getPostVar('hide_smilies',0);
			$post_id = Agora::getPostVar('post_id');
			$this->id = $post_id;

			$parser = & $this->helper('parser');
			$message = $parser->parseMessage($message, $hide_smilies);

			$signature = '';
			if ($this->agora_user['show_sig']) {
				$signature = $parser->parseSignature($this->agora_user['signature']);
			}

			if ($post_id) {
				$post_model = &Model::getInstance('PostModel');
				$post = $post_model->load($post_id);

				$user_model = &Model::getInstance('UserModel');
				$post['user'] = $user_model->load($post['poster_id']);
			} else {
				$post = array();
				$post['user'] = $this->agora_user;
				$post['id'] = 0;
				
				$post['poster_id'] = $this->agora_user['id'];
				$post['poster'] = $this->agora_user['username'];
				$post['posted'] = time();
			}

			$user_helper = &$this->helper('user');

			$post['user'] = $user_helper->prepareUserAvatar($post['user']);
			$post['user'] = $user_helper->prepareUserTitle($post['user']);
			
			if ($this->id) {
				$this->authenticatePost($this->id,'edit_posts');
				$this->view->assign('post_id',$this->id);
			}

			$post['message'] = $message;
			$post['user']['signature'] = $signature;

			$this->view->assignRef('post',$post);
			$this->view->assign('access',array());
			$this->view->assign('post_num','-');
			$this->view->assign('topic_id',$this->id);

			$this->view->assign('raw_message',$raw_message);
			$this->view->assign('hide_smilies',$hide_smilies);

/*			$this->view->assign('merge',$merge);
			$this->view->assign('subscribe',$subscribe);*/

			$this->view->template = 'post/preview';
			
			$this->display();
			jexit(0);
		}

		function saved() {
			$this->_default();
		}

		function _save() {
			$can_edit = $this->access_model->authenticatePost($this->agora_user['id'], $this->id,'edit_posts');
			
			if ($this->agora_user['is_guest']) {
				$captcha = Agora::getPostVar('captcha');
				$c_key = Agora::getSessionVar('captcha');

				if (trim($captcha) !== trim($c_key) || !$captcha) {
					Agora::showMessage(Agora::lang('Bad captcha'));
					Agora::redirect(Agora::getRefferer());
				}
			}

			$post_model = & Model::getInstance('PostModel');
			$post = $post_model->load($this->id);

			if (!$post) {
				Agora::show404();
				Agora::redirect(Agora::makeURL(''));
			}
			// member cannot edit other users posts posts
			if (!$this->agora_user['is_superadmin'] && !$can_edit &&  $post['poster_id'] != $this->agora_user['id']) {
				Agora::showMessage(Agora::lang('Access denied'));
				Agora::redirect(Agora::getRefferer());
			}
			
			$message = Agora::getPostVar('req_message','', true);
			$parser = & $this->helper('parser');
			$message = $parser->fix_broken_bbcode($message);
			
			$silent = Agora::getPostVar('silent','0');
			$post_model->edit($post['id'],$message, $silent, $this->agora_user['username'], $post['poster']);
			
			$topic_model = & Model::getInstance('TopicModel');
			$topic = $topic_model->load($post['topic_id']);
			
			$reverse = (int) $this->agora_user['reverse_posts'];
			$per_page = intval($this->agora_user['disp_posts']) > 0 ? $this->agora_user['disp_posts'] : intval($this->agora_config['o_disp_posts_default']);
			
			$feed_model = & Model::getInstance('FeedModel');
			$all_posts = $feed_model->getTopicAll($post['topic_id']);
			
			$num_posts = count($all_posts);
			$num = array_search($post['id'], $all_posts);
			$current = (!$reverse) ? ($num_posts - $num) : ($num + 1);			
			$page = ceil($current / $per_page);
			
			$url = array('task' => 'topic', 'id' => $post['topic_id'], 'p' => $page);
			
            if ( strpos($_SERVER['PHP_SELF'],'index2.php') != 0 ) {
				$mode = 'agroup';
			} else {
				$mode = '';
			}
			
            if ('agroup' == $mode) {
				Agora::redirect(Agora::makeURL($url,true,true). '#p'.$this->id);
			} else {
				Agora::redirect(Agora::makeURL($url). '#p'.$this->id);
			}

		}
		
	}

